package service

import (
	"anban/models"
	"anban/utils"
	"fmt"
	"github.com/astaxie/beego/orm"
	"net/url"
)

// 获取学生作业通知信息
func GetHomeworkNoticeInfo(p map[string]interface{}) *models.HomeworkNotice {
	o := orm.NewOrm()
	homeworkNotice := &models.HomeworkNotice{}
	qs := o.QueryTable("HomeworkNotice")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["id"]; ok {
		qs = qs.Filter("id", p["id"])
	}
	qs.One(homeworkNotice)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		if homeworkNotice.Id > 0 {
			homeworkNotice.CreatedShowDate = homeworkNotice.Created.Format("2006.1.2")
			homeworkNotice.CreatedShow = homeworkNotice.Created.Format("2006-01-02 15:04:05")
		}
	}
	return homeworkNotice
}

// 添加单个学生作业通知
func AddHomeworkNotice(input url.Values) (int64, error) {
	o := orm.NewOrm()
	homeworkNotice := &models.HomeworkNotice{}
	student := &models.Student{}
	if _, ok := input["student_id"]; ok {
		student.Id = utils.Atoi64(input["student_id"][0])
		o.Read(student)
	}
	homeworkNotice.Student = student
	homework := &models.Homework{}
	if _, ok := input["homework_notice_id"]; ok {
		homeworkNotice.Id = utils.Atoi64(input["homework_notice_id"][0])
		o.Read(homeworkNotice)
	}
	homeworkNotice.Homework = homework
	return o.Insert(homeworkNotice)
}

// 添加多个学生作业通知
func AddHomeworkNotices(input url.Values) (int64, error) {
	o := orm.NewOrm()
	var homeworkNotices []models.HomeworkNotice
	homework := &models.Homework{}
	if _, ok := input["homework_id"]; ok {
		homework.Id = utils.Atoi64(input["homework_id"][0])
		o.Read(homework)
	}
	p := map[string]interface{}{}
	p["classId"] = input["class_id"][0]
	_, studentList := GetStudentList(p)
	for _, student := range studentList {
		if student.Status == 1 {
			homeworkNotice := models.HomeworkNotice{}
			homeworkNotice.Homework = homework
			homeworkNotice.Student = student
			homeworkNotices = append(homeworkNotices, homeworkNotice)
		}
	}
	return o.InsertMulti(1, homeworkNotices)
}

// 修改学生作业通知信息
func EditHomeworkNotice(input url.Values) (int64, error) {
	o := orm.NewOrm()
	p := orm.Params{}
	for k, v := range input {
		if k == "id" || k == "_xsrf" {
			continue
		}
		p[k] = v[0]
	}
	return o.QueryTable("HomeworkNotice").Filter("id", input["id"][0]).Update(p)
}

// 获取学生作业通知列表
func GetHomeworkNoticeList(p map[string]interface{}) (int64, []*models.HomeworkNotice) {
	var homeworkNoticeList []*models.HomeworkNotice
	o := orm.NewOrm()
	qs := o.QueryTable("HomeworkNotice")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["studentIds"]; ok && len(p["studentIds"].([]int64)) > 0 {
		qs = qs.Filter("student_id__in", p["studentIds"])
	}
	totalCount, _ := qs.Count()
	curPage, ok := p["curPage"]
	perCount, _ := p["perCount"]
	if _, isLimit := p["limit"]; isLimit {
		qs = qs.Limit(p["limit"])
	} else if ok {
		start := perCount.(int) * (curPage.(int) - 1)
		qs = qs.Limit(perCount, start)
	}
	qs.OrderBy("-id").All(&homeworkNoticeList)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		for _, homeworkNotice := range homeworkNoticeList {
			homeworkNotice.CreatedShowDate = homeworkNotice.Created.Format("2006.1.2")
			homeworkNotice.CreatedShow = homeworkNotice.Created.Format("2006-01-02 15:04:05")
		}
	}
	if _, ok := p["api"]; ok && p["api"].(bool) {
		for _, homeworkNotice := range homeworkNoticeList {
			homeworkNotice.StudentName = homeworkNotice.Student.RealName
			homeworkNotice.TeacherName = homeworkNotice.Homework.User.RealName
			homeworkNotice.TeacherPhone = homeworkNotice.Homework.User.Phone
			homeworkNotice.HomeworkType = fmt.Sprintf("%s作业", homeworkNotice.Homework.Course.Name)
			homeworkNotice.HomeworkContent = homeworkNotice.Homework.Content
			homeworkNotice.CreatedShowDate = homeworkNotice.Created.Format("2006.1.2")
		}
	}
	return totalCount, homeworkNoticeList
}
